package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.mapper.CategoryAttributeTemplateMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.CategoryAttributeTemplateAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.CategoryAttributeTemplate;
import cn.tedu.csmall.product.pojo.vo.CategoryAttributeTemplateStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryAttributeTemplateService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class CategoryAttributeTemplateService implements ICategoryAttributeTemplateService, ServiceCode {
  @Autowired
  CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;
  @Autowired
  CategoryMapper categoryMapper;
  @Autowired
  AttributeTemplateMapper attributeTemplateMapper;
    public CategoryAttributeTemplateService() {
        log.debug("创建业务类:CategoryAttributeTemplateService");
    }

    @Override
    public void andNew(CategoryAttributeTemplateAndNewDTO categoryAttributeTemplateAndNewDTO) {
        log.debug("开始处理【添加类别与属性模板的关联】的业务，参数：{}", categoryAttributeTemplateAndNewDTO);
        Long categoryId = categoryAttributeTemplateAndNewDTO.getCategoryId();
        Long attributeTemplateId = categoryAttributeTemplateAndNewDTO.getAttributeTemplateId();
        // 检查类别是否存在

        {
            Object queryResult = categoryMapper.getStandardById(categoryId);
            if (queryResult == null) {
                String message = "添加类别与属性模板的关联失败！类别不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        // 检查属性模板是否存在
        {
            Object queryResult = attributeTemplateMapper.getById(attributeTemplateId);
            if (queryResult == null) {
                String message = "添加类别与属性模板的关联失败！属性模板不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        // 检查关联是否已经存在
        {
            int count = categoryAttributeTemplateMapper.countByCategoryAndAttributeTemplate(categoryId, attributeTemplateId);
            if (count != 0) {
                String message = "添加类别与属性模板的关联失败！此关联关系已经存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        // 创建对象
        CategoryAttributeTemplate categoryAttributeTemplate = new CategoryAttributeTemplate();
        // 复制属性
        BeanUtils.copyProperties(categoryAttributeTemplateAndNewDTO, categoryAttributeTemplate);
        // 执行插入数据
        int rows = categoryAttributeTemplateMapper.insert(categoryAttributeTemplate);
        if (rows != 1) {
            String message = "添加类别与属性模板的关联失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
    @Override
    public void delete(Long id) {
        log.debug("开始处理删除功能，删除id:{}",id);
        CategoryAttributeTemplateStandardVO catById = categoryAttributeTemplateMapper.getCATById(id);
        if (catById==null){
            String message="删除数据失败！该数据"+id+"不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int delete = categoryAttributeTemplateMapper.delete(id);
        if (delete!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
